package cn.itcast.babasport.service.brand;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.babasport.mapper.product.BrandMapper;
import cn.itcast.babasport.pojo.page.Pagination;
import cn.itcast.babasport.pojo.product.Brand;
import cn.itcast.babasport.pojo.product.BrandQuery;
@Service("brandService")
public class BrandServiceImpl implements BrandService {
	
	@Resource
	private BrandMapper brandMapper;
	
	@Override
	public List<Brand> selectBrandListNoPage(String name, Integer isDisplay) {
		BrandQuery brandQuery = new  BrandQuery();
		if (name != null && !"".equals(name)) {
			brandQuery.setName(name);
		}
		if (isDisplay != null) {
			brandQuery.setIsDisplay(isDisplay);
		}
		return brandMapper.selectBrandListNoPage(brandQuery);
	}
	/**
	 * 查询商品分类信息分页查询
	 */
	@Override
	public Pagination selectBrandListHavePage(String name, Integer isDisplay, Integer pageNo) {
		// 创建BrandQuery对象，封装查询条件
				BrandQuery brandQuery = new BrandQuery();
				// 封装分页工具栏需要的参数
				StringBuilder params = new StringBuilder();
				if(name != null && !"".equals(name)){
					brandQuery.setName(name);
					params.append("name=").append(name);
				}
				if(isDisplay != null){
					brandQuery.setIsDisplay(isDisplay);
					params.append("&isDisplay=").append(isDisplay);  // ？&isDisplay=1 正确
				}
				brandQuery.setPageNo(Pagination.cpn(pageNo)); // 当前页码
				brandQuery.setPageSize(5); // 每页显示条数
				
				// 查询
				List<Brand> list = brandMapper.selectBrandListHavePage(brandQuery);
				int totalCount = brandMapper.selectBrandCount(brandQuery);
				// 创建分页对象并且填充数据
				Pagination pagination = new Pagination(brandQuery.getPageNo(), brandQuery.getPageSize(), totalCount, list);
				// 构建分页工具栏
				String url = "/brand/list.do";
				pagination.pageView(url, params.toString());
				return pagination;
	}
	/**
	 * 根据ID查询商品分类
	 */
	@Override
	public Brand selectBrandById(Long id) {
		return brandMapper.selectBrandById(id);
	}
	/**
	 * 更新商品分类
	 */
	@Transactional
	@Override
	public void updateBrand(Brand brand) throws Exception {
		brandMapper.updateBrand(brand);
	}
	/**
	 * 添加商品分类信息
	 */
	@Override
	public void insertBrand(Brand brand) {
		brandMapper.insertBrand(brand);
	}
	/**
	 * 批量删除商品分类信息
	 */
	@Override
	public void deleteBatchBrand(Long[] ids) {
		brandMapper.deleteBatchBrand(ids);
		
	}

}
